package es.kivitel.android.sms.gui;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.Toast;
import es.kivitel.android.sms.R;
import es.kivitel.android.sms.kernel.EmailEvent;
import es.kivitel.android.sms.kernel.Event;
import es.kivitel.android.sms.kernel.SmsEvent;
import es.kivitel.android.sms.utils.DateSerializer;

public class NewEventActivity extends Activity{
	private AlertDialog mAlert;
	protected DatePicker mDp;
	protected TimePicker mTp;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle){
       super.onCreate(icicle);
       _loadNewEventWindow();
    }
   
    private void _loadNewEventWindow(){
        setContentView(R.layout.addeventdate);
        //Store values date and time picker values
       // final CharSequence[] items = {"sms", "mms", "mail"};
        final CharSequence[] items = {"sms", "mail"};
       AlertDialog.Builder builder = new AlertDialog.Builder(this);
       builder.setTitle("Message type");
       //Toast.makeText(getApplicationContext(),"LLEGA AQUI", Toast.LENGTH_LONG).show();
 
       builder.setItems(items, new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int item) {
               //Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
           	switch(item){
           		case 0:
           			setContentView(R.layout.addeventsms);
           	        final Button buttonSendSMS = (Button) findViewById(R.id.btoksms);
           	        buttonSendSMS.setOnClickListener(new View.OnClickListener() {
           	        	// Perform action on click
           	            public void onClick(View v) {
           	            	addSMSEvent();           	
           	            } 
           	        });  
           			break;
/*
 //Disable send MMS on this release
 				case 1:
           			setContentView(R.layout.addeventmms);
           	        final Button buttonSendMMS = (Button) findViewById(R.id.btokmms);
           	        buttonSendMMS.setOnClickListener(new View.OnClickListener() {
           	        	// Perform action on click
           	            public void onClick(View v) {
           	            	addMMSEvent();           	
           	            } 
           	        });              			
           			break;
*/
           		case 1:
           			setContentView(R.layout.addeventmail);
           	        final Button buttonSendMail = (Button) findViewById(R.id.btokmail);
           	        buttonSendMail.setOnClickListener(new View.OnClickListener() {
           	        	// Perform action on click
           	            public void onClick(View v) {
           	            	addMailEvent();           	
           	            } 
           	        });             			
           			break;            			
           		default:
           			break;
           	}
           }
       });
       mAlert = builder.create();

       
       //After insert date, choose a type of message at mAlert
       final Button buttonDate = (Button) findViewById(R.id.btokdate);
       buttonDate.setOnClickListener(new View.OnClickListener() {
       	// Perform action on click
           public void onClick(View v) {
           	mDp = (DatePicker)findViewById(R.id.datepick);
           	mTp = (TimePicker)findViewById(R.id.timepick);
            mAlert.show();            	
           } 
       });
             	
   }    
    


    private void addSMSEvent(){
    	if( _testSMSInput() ){
    		EditText telephone = (EditText) findViewById(R.id.number); 
            EditText text = (EditText) findViewById(R.id.text);    	
            CheckBox cb = (CheckBox) findViewById(R.id.CBAuthorization);
    		
    		DateSerializer ds = new DateSerializer( mDp, mTp );

    		SmsEvent ev = new SmsEvent( );
    		
    	
    		ev.setSendingDate(ds.getSerializedDate());
    		ev.setDestination( telephone.getText().toString() );
    		ev.setContent( text.getText().toString() ); 
    		
    		if( cb.isChecked() ) ev.setMode(Event.FOREGROUND_MODE);
    		else ev.setMode(Event.BACKGROUND_MODE);
    		
    		try {
				ev.store( this );
			} catch (Exception e) {
				Toast.makeText(this,"ERROR: unable to store message", Toast.LENGTH_LONG).show();
				e.printStackTrace();
			}
    		
    		_exit();
    	}
    }
    
/*    
    private void addMMSEvent(){
    	if( _testMMSInput() ) _exit();
    }
*/
    private void addMailEvent(){
    	if( _testMailInput() ){
    		EditText address = (EditText) findViewById(R.id.address); 
    		EditText subject = (EditText) findViewById(R.id.subject); 
    		EditText text = (EditText) findViewById(R.id.text);    	
            CheckBox cb = (CheckBox) findViewById(R.id.CBAuthorization);    		
    		
    		DateSerializer ds = new DateSerializer( mDp, mTp );

    		EmailEvent ev = new EmailEvent( );
    		
    	
    		ev.setSendingDate(ds.getSerializedDate());
    		ev.setDestination( address.getText().toString() );
    		ev.setSubject( subject.getText().toString() );
    		ev.setContent( text.getText().toString() ); 
    		//ev.setAttachedFile( attachedFile );
    		
    		if( cb.isChecked() ) ev.setMode(Event.FOREGROUND_MODE);
    		else ev.setMode(Event.BACKGROUND_MODE);
    		
    		try {
				ev.store( this );
			} catch (Exception e) {
				Toast.makeText(this,"ERROR: unable to store message", Toast.LENGTH_LONG).show();
				e.printStackTrace();
			}    		
    		_exit();
    	}
    }
    
    //Test inputs before sending messages
    private boolean _testSMSInput(){
    	boolean ret = true;
        EditText telephone = (EditText) findViewById(R.id.number); 
        
        if(telephone.getText().toString().trim().length() == 0){
        	ret = false;
        	Toast.makeText(getApplicationContext(),"wrong telephone Number", Toast.LENGTH_SHORT).show();
        }
        
    	return ret;
    }
    
 /*
    private boolean _testMMSInput(){
    	boolean ret = true;
        EditText telephone = (EditText) findViewById(R.id.number); 
        
        if(telephone.getText().toString().trim().length() == 0){
        	ret = false;
        	Toast.makeText(getApplicationContext(),"wrong telephone Number", Toast.LENGTH_SHORT).show();
        }
 
        EditText subject = (EditText) findViewById(R.id.subject); 
        EditText text = (EditText) findViewById(R.id.text);
        
        if( ret &&
        	subject.getText().toString().trim().length() == 0 &&
        	text.getText().toString().trim().length() == 0 ){
        	ret = false;
        	Toast.makeText(getApplicationContext(), "fill subject and/or text", Toast.LENGTH_LONG).show();        	
        }

    	return ret;    	
    }
*/
    
    private boolean _testMailInput(){
    	boolean ret = true;
        EditText address = (EditText) findViewById(R.id.address); 
        
        if( address.getText().toString().trim().length() == 0 ||
        	address.getText().toString().indexOf("@") == -1){
        	ret = false;
        	Toast.makeText(getApplicationContext(),"wrong email address", Toast.LENGTH_SHORT).show();
        }
 
        EditText subject = (EditText) findViewById(R.id.subject); 
        EditText text = (EditText) findViewById(R.id.text);
        
        if( ret &&
        	subject.getText().toString().trim().length() == 0 &&
        	text.getText().toString().trim().length() == 0 ){
        	ret = false;
        	Toast.makeText(getApplicationContext(), "fill subject and/or text", Toast.LENGTH_LONG).show();        	
        }

    	return ret;   
    }
    
    private void _exit(){
    	this.finish();
    }
    
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    	super.onActivityResult(requestCode, resultCode, data);
    	Bundle extras = data.getExtras();
    	if(extras.getInt("Close") == 1)
    		finish();  	
    }
	    
}
